package com.pt.manage.service.impl;
import com.pt.common.utils.AiString;
import com.pt.manage.deepseek.DeepSeekConfig;
import com.pt.manage.domain.ApiResponse;
import com.pt.manage.domain.DeepSeekRequest;
import com.pt.manage.domain.DeepSeekResponse;
import com.theokanning.openai.completion.chat.ChatCompletionResult;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;


@Service
public class DeepSeekService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DeepSeekConfig deepSeekConfig;

    private final ObjectMapper objectMapper = new ObjectMapper();

    public ApiResponse askDeepSeek(String question) throws JsonProcessingException {

        DeepSeekRequest request = new DeepSeekRequest();
        request.setModel("gpt-3.5-turbo");
        request.setStream(false);

        CopyOnWriteArrayList<DeepSeekRequest.Message> messages = new CopyOnWriteArrayList<>();
        messages.add(new DeepSeekRequest.Message("user", "我将给你发一段我的作业，你需要给出评分和评语，评分满分100分，评语200字左右"
        +"回答格式是这样的，scores:你给出的分数" +
                "Comments:你给出的评语"+"例如Socres: 80。Comments: 您提交的这篇文章主题明确，内容详实，通过具体事例展示了论点，论据充分，分析到位。文章结构合理，逻辑清晰，层次分明，使读者能够轻松跟随您的思路。在语言表达上，您使用了丰富的词汇和恰当的修辞手法，使得文章读起来生动有趣，引人入胜。不过，部分段落之间的衔接还有提升空间，建议在过渡句的使用上多下功夫，以增强文章的连贯性。此外，部分论点可以进一步深化，提供更多独特见解或例证。总体来说，这是一篇质量上乘的文章，稍加打磨便可更加出色"
        ));

        messages.add(new DeepSeekRequest.Message("user", question));
        request.setMessages(messages);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + deepSeekConfig.getApiKey());
        HttpEntity<String> entity = new HttpEntity<>(objectMapper.writeValueAsString(request), headers);

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(deepSeekConfig.getApiUrl());

        ResponseEntity<DeepSeekResponse> response = restTemplate.postForEntity(builder.toUriString(), entity, DeepSeekResponse.class);

        if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
            DeepSeekResponse Response = response.getBody();

            Map<String,String> m =  AiString.parseEvaluation(Response.getChoices().get(0).getMessage().getContent());
            ApiResponse ar = new ApiResponse(m.get("scores"),m.get("coments"));

            return ar;
        } else {
            throw new RuntimeException("API调用失败: " + response.getStatusCode());
        }
    }
}
